﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Data.Odbc;
using Reviewer.Entities;
using Reviewer.Business;

public partial class MidtDataFeed : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Clear();

        Response.ContentType = "text/xml";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetAllowResponseInBrowserHistory(false);
        Response.Cache.SetExpires(DateTime.Now);

        XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;
        writer.WriteStartDocument();
        writer.WriteDocType("midtdata", null, null, null);
        writer.WriteStartElement("midtdata");
        writer.WriteStartElement("Sources");

        writer.WriteStartElement("Source");
        writer.WriteElementString("Source_Name", "Reviewer.dk");
        writer.WriteElementString("Language", "da-DK");
        writer.WriteElementString("SourceURL", "http://www.reviewer.dk");

        writer.WriteStartElement("Products");

        IEnumerable<Review> reviews = ServiceFactory.ReviewService.Find().OrderBy(t => t.Id).Reverse();

        foreach (Review review in reviews)
        {
            Game currentGame = ServiceFactory.GameService.GetBy(review.GameId);
            if (currentGame == null)
            {
                continue;
            }
            Format currentFormat = ServiceFactory.GetMetadataService<Format>().GetAll().FirstOrDefault(t => t.Id.Equals(currentGame.Format));
            if (currentFormat == null)
            {
                continue;
            }

            writer.WriteStartElement("Product");

            writer.WriteElementString("InternalProductIdentifier", review.Id.ToString());
            writer.WriteElementString("Source_name", "Reviewer.dk");
            writer.WriteElementString("ProductName", currentGame.Title);
            writer.WriteElementString("ProductManufacturer", currentGame.Publisher);
            writer.WriteElementString("OriginalCategoryName", "Review");
            writer.WriteElementString("TestUrl", "http://www.reviewer.dk" + UrlUtillity.GetReviewUri(currentFormat.Name, currentGame.Title) + "&utm_medium=affiliate&utm_source=midtdata");

            writer.WriteStartElement("Product_ids");
            writer.WriteStartElement("Product_id");
            writer.WriteElementString("ID_kind", "GameFormat");
            writer.WriteElementString("ID_value", currentFormat.Name);
            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.WriteEndElement();
        }

        writer.WriteEndDocument();
        writer.Flush();
        //Response.Flush();
        //Response.End();
    }
}
